package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.work.enums.DiagnosisReportStatus;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.DiagnosisReportDao;
import com.jiantong.work.model.DiagnosisReport;
import com.jiantong.work.service.DiagnosisReportReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * Created by maowenjie on 2017/3/27.
 */
@Slf4j
@Service
@RpcProvider
public class DiagnosisReportReadServiceImpl implements DiagnosisReportReadService{
    private final DiagnosisReportDao diagnosisReportDao;
    private final BreedFactoryDao breedFactoryDao;

    @Autowired
    public DiagnosisReportReadServiceImpl(DiagnosisReportDao diagnosisReportDao, BreedFactoryDao breedFactoryDao){
        this.diagnosisReportDao = diagnosisReportDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    @Override
    public Response<DiagnosisReport> findById(Long id) {

        try {
            DiagnosisReport diagnosisReport = diagnosisReportDao.findById(id);
            if (diagnosisReport == null) {
                log.error("DiagnosisReport(id={}) is not found", id);
                return Response.fail("diagnosisReport.not.found");
            }
            return Response.ok(diagnosisReport);
        } catch (Exception e) {
            log.error("failed to find diagnosisReport(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("diagnosisReport.find.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = diagnosisReportDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            if(status == null){
                log.error("diagnosisReport status (id={}) is not found", id);
                return Response.fail("diagnosisReport.status.not.found");
            }
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("diagnosisReport status (id={}) is not found", id);
            return Response.fail("diagnosisReport.status.not.found");
        }
    }

    @Override
    public Response<Paging<DiagnosisReport>> pagingDiagnosisReport(Integer pageNo, Integer pageSize, Integer status,Long breedFactoryId) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if (status != null) {
                criteria.put("status", status);
            }
            criteria.put("breedFactoryId",breedFactoryId);
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(diagnosisReportDao.paging(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find diagnosisReport(id={}), cause:{}", status, Throwables.getStackTraceAsString(e));
            return Response.fail("diagnosisReport.find.fail");
        }
    }

    @Override
    public Response<Paging<DiagnosisReport>> pagingDiagnosisReportGqv(Integer pageNo, Integer pageSize, Long breedFactoryId,Integer breedType, Date fromDate, Date toDate) {
        try {
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            Map<String, Object> criteria = Maps.newHashMap();

            List<Long> breedFactoryIds = new ArrayList<>();

            if(breedFactoryId != null){
                breedFactoryIds.add(breedFactoryId);
            }else {
                LoginUser loginUser = UserUtil.getCurrentUser();
                breedFactoryIds = loginUser.getBreedFactoryIds();
            }
            if (breedType != null) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("breedType",breedType);
                if(!breedFactoryIds.isEmpty()){
                    map.put("breedFactoryIds",breedFactoryIds);
                    breedFactoryIds = breedFactoryDao.selectedByType(map);
                }
            }

            if(!breedFactoryIds.isEmpty()) criteria.put("breedFactoryIds", breedFactoryIds);
            else return Response.ok(new Paging<>(0L,new ArrayList<>()));

            if(fromDate != null) criteria.put("fromDate", fromDate);

            if(toDate != null) criteria.put("toDate",toDate);
            criteria.put("status", DiagnosisReportStatus.FINISHED.value());
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return  Response.ok(diagnosisReportDao.pagingDiagnosisReportGqv(pageInfo.getOffset(), pageInfo.getLimit(), criteria));
        } catch (Exception e) {
            log.error("failed to find DiagnosisReport fromDate={},fromDate={},toDate={}),cause:{}", fromDate,toDate,Throwables.getStackTraceAsString(e));
            return Response.fail("DiagnosisReport.find.fail");
        }
    }

    @Override
    public Response<Paging<DiagnosisReport>> pagingDiagnosisReportGqv(Integer pageNo, Integer pageSize, Map map) {

        try{
            PageInfo pageInfo = new PageInfo(pageNo, pageSize);
            return  Response.ok(diagnosisReportDao.pagingDiagnosisReportGqv(pageInfo.getOffset(), pageInfo.getLimit(), map));
        }catch(Exception e){
            log.error("failed to find DiagnosisReport fromDate={},fromDate={},toDate={}),cause:{}",map,Throwables.getStackTraceAsString(e));
            return Response.fail("DiagnosisReport.find.fail");
        }

    }

    @Override
    public Response<Integer> countDiagnosisTimesForWeb(Map<String, Object> map) {
        try{
            return Response.ok(diagnosisReportDao.countDiagnosisTimesForWeb(map));
        }catch (Exception e){
            log.error("failed to count times , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to count times");
        }
    }

    @Override
    public Response<List<Map<String, Object>>> findDiagnosisInfoForWeb(Map<String, Object> map) {
        try{
            return Response.ok(diagnosisReportDao.findDiagnosisInfoForWeb(map));
        }catch (Exception e){
            log.error("failed to find diagnosis info , case = {}",Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find diagnosis info");
        }
    }
}
